<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 1. 需要一些可以改变url的按钮,a标签 -->
    <p>
      <a href="#hot">热映</a> | <a href="#cinema">影院</a> |
      <a href="#wait">待映</a> |
      <a href="#classic">经典电影</a>
    </p>

    <!-- 2. 需要一个存放切换内容的容器 -->
    <div id="box"></div>

    <script>
      // 3. 需要一个路由表，就是一个数组，里面每一项是对象，path,template
      const routes = [
        {
          path: "hot",
          template: "<div>这是热映页面</div>",
        },
        {
          path: "cinema",
          template: "<div>这是影院页面</div>",
        },
        {
          path: "wait",
          template: "<div>这是待映页面</div>",
        },
        {
          path: "classic",
          template: "<div>这是经典电影页面</div>",
        },
      ];

      // 4. 监听url的改变
      window.addEventListener("hashchange", () => {
        // console.log("hash改变了");
        const hash = location.hash.slice(1);

        routes.forEach((item) => {
          if (item.path === hash) {
            document.querySelector("#box").innerHTML = item.template;
          }
        });
      });
    </script>
  </body>
</html>
